home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  6.1 KB  |  269 lines

  1. /* Send and receive User Datagram Protocol packets */
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "netuser.h"
  5. #include "iface.h"
  6. #include "udp.h"
  7. #include "ip.h"
  8. #include "internet.h"
  9. #include "icmp.h"
  10.  
  11. static struct udp_cb *lookup_udp __ARGS((struct socket *socket));
  12. static int16 hash_udp __ARGS((struct socket *socket));
  13.  
  14. struct mib_entry Udp_mib[] = {
  15.     "",            0,
  16.     "udpInDatagrams",    0,
  17.     "udpNoPorts",        0,
  18.     "udpInErrors",        0,
  19.     "udpOutDatagrams",    0,
  20. };
  21.  
  22. /* Hash table for UDP structures */
  23. struct udp_cb *Udps[NUDP] = { NULLUDP} ;
  24.  
  25. /* Create a UDP control block for lsocket, so that we can queue
  26.  * incoming datagrams.
  27.  */
  28. struct udp_cb *
  29. open_udp(lsocket,r_upcall)
  30. struct socket *lsocket;
  31. void (*r_upcall)();
  32. {
  33.     register struct udp_cb *up;
  34.     int16 hval; 
  35.  
  36.     if((up = lookup_udp(lsocket)) != NULLUDP)
  37.         return up;    /* Already exists */
  38.     up = (struct udp_cb *)callocw(1,sizeof (struct udp_cb));
  39.     up->socket.address = lsocket->address;
  40.     up->socket.port = lsocket->port;
  41.     up->r_upcall = r_upcall;
  42.  
  43.     hval = hash_udp(lsocket);
  44.     up->next = Udps[hval];
  45.     if(up->next != NULLUDP)
  46.         up->next->prev = up;
  47.     Udps[hval] = up;
  48.     return up;
  49. }
  50.  
  51. /* Send a UDP datagram */
  52. int
  53. send_udp(lsocket,fsocket,tos,ttl,data,length,id,df)
  54. struct socket *lsocket;        /* Source socket */
  55. struct socket *fsocket;        /* Destination socket */
  56. char tos;            /* Type-of-service for IP */
  57. char ttl;            /* Time-to-live for IP */
  58. struct mbuf *data;        /* Data field, if any */
  59. int16 length;            /* Length of data field */
  60. int16 id;            /* Optional ID field for IP */
  61. char df;            /* Don't Fragment flag for IP */
  62. {
  63.     struct mbuf *bp;
  64.     struct pseudo_header ph;
  65.     struct udp udp;
  66.     int32 laddr;
  67.  
  68.     if(length != 0 && data != NULLBUF)
  69.         trim_mbuf(&data,length);
  70.     else
  71.         length = len_p(data);
  72.  
  73.     length += UDPHDR;
  74.  
  75.     laddr = lsocket->address;
  76.     if(laddr == INADDR_ANY)
  77.         laddr = locaddr(fsocket->address);
  78.  
  79.     udp.source = lsocket->port;
  80.     udp.dest = fsocket->port;
  81.     udp.length = length;
  82.  
  83.     /* Create IP pseudo-header, compute checksum and send it */
  84.     ph.length = length;
  85.     ph.source = laddr;
  86.     ph.dest = fsocket->address;
  87.     ph.protocol = UDP_PTCL;
  88.  
  89.     if((bp = htonudp(&udp,data,&ph)) == NULLBUF){
  90.         Net_error = NO_MEM;
  91.         free_p(data);
  92.         return 0;
  93.     }
  94.     udpOutDatagrams++;
  95.     ip_send(laddr,fsocket->address,UDP_PTCL,tos,ttl,bp,length,id,df);
  96.     return (int)length;
  97. }
  98. /* Accept a waiting datagram, if available. Returns length of datagram */
  99. int
  100. recv_udp(up,fsocket,bp)
  101. register struct udp_cb *up;
  102. struct socket *fsocket;        /* Place to stash incoming socket */
  103. struct mbuf **bp;        /* Place to stash data packet */
  104. {
  105.     struct socket *sp;
  106.     struct mbuf *buf;
  107.     int16 length;
  108.  
  109.     if(up == NULLUDP){
  110.         Net_error = NO_CONN;
  111.         return -1;
  112.     }
  113.     if(up->rcvcnt == 0){
  114.         Net_error = WOULDBLK;
  115.         return -1;
  116.     }
  117.     buf = dequeue(&up->rcvq);
  118.     up->rcvcnt--;
  119.  
  120.     sp = (struct socket *)buf->data;
  121.     /* Fill in the user's foreign socket structure, if given */
  122.     if(fsocket != NULLSOCK){
  123.         fsocket->address = sp->address;
  124.         fsocket->port = sp->port;
  125.     }
  126.     /* Strip socket header and hand data to user */
  127.     pullup(&buf,NULLCHAR,sizeof(struct socket));
  128.     length = len_p(buf);
  129.     if(bp != (struct mbuf **)NULL)
  130.         *bp = buf;
  131.     else
  132.         free_p(buf);
  133.     return (int)length;
  134. }
  135. /* Delete a UDP control block */
  136. int
  137. del_udp(up)
  138. struct udp_cb *up;
  139. {
  140.     struct mbuf *bp;
  141.     int16 hval;
  142.  
  143.     if(up == NULLUDP){
  144.         Net_error = INVALID;
  145.         return -1;
  146.     }        
  147.     /* Get rid of any pending packets */
  148.     while(up->rcvcnt != 0){
  149.         bp = up->rcvq;
  150.         up->rcvq = up->rcvq->anext;
  151.         free_p(bp);
  152.         up->rcvcnt--;
  153.     }
  154.     hval = hash_udp(&up->socket);
  155.     if(up->prev == NULLUDP)
  156.         Udps[hval] = up->next;        /* First on list */
  157.     else
  158.         up->prev->next = up->next;
  159.     if(up->next != NULLUDP)
  160.         up->next->prev = up->prev;
  161.  
  162.     free((char *)up);
  163.     return 0;
  164. }
  165. /* Process an incoming UDP datagram */
  166. void
  167. udp_input(iface,bp,ip,rxbroadcast)
  168. struct iface *iface;    /* Input interface */
  169. struct mbuf *bp;    /* UDP header and data */
  170. struct ip *ip;        /* IP header */
  171. int rxbroadcast;    /* The only protocol that accepts 'em */
  172. {
  173.     struct pseudo_header ph;
  174.     struct udp udp;
  175.     struct udp_cb *up;
  176.     struct socket lsocket;
  177.     struct socket *fsocket;
  178.     struct mbuf *packet;
  179.     int ckfail = 0;
  180.     int16 length;
  181.  
  182.     if(bp == NULLBUF)
  183.         return;
  184.  
  185.     /* Create pseudo-header and verify checksum */
  186.     ph.source = ip->source;
  187.     ph.dest = ip->dest;
  188.     ph.protocol = ip->protocol;
  189.     length = ip->length - IPLEN - ip->optlen;
  190.     ph.length = length;
  191.  
  192.     if(cksum(&ph,bp,length) != 0)
  193.         /* Checksum apparently failed, note for later */
  194.         ckfail++;
  195.  
  196.     /* Extract UDP header in host order */
  197.     ntohudp(&udp,&bp);
  198.  
  199.     /* If the checksum field is zero, then ignore a checksum error.
  200.      * I think this is dangerously wrong, but it is in the spec.
  201.      */
  202.     if(ckfail && udp.checksum != 0){
  203.         udpInErrors++;
  204.         free_p(bp);
  205.         return;
  206.     }
  207.     /* If this was a broadcast packet, pretend it was sent to us */
  208.     if(rxbroadcast){
  209.         lsocket.address = iface->addr;
  210.     } else
  211.         lsocket.address = ip->dest;
  212.  
  213.     lsocket.port = udp.dest;
  214.     /* See if there's somebody around to read it */
  215.     if((up = lookup_udp(&lsocket)) == NULLUDP){
  216.         /* Nope, return an ICMP message */
  217.         if(!rxbroadcast){
  218.             bp = htonudp(&udp,bp,&ph);
  219.             icmp_output(ip,bp,ICMP_DEST_UNREACH,ICMP_PORT_UNREACH,NULL);
  220.         }
  221.         udpNoPorts++;
  222.         free_p(bp);
  223.         return;
  224.     }
  225.     /* Create space for the foreign socket info */
  226.     if((packet = pushdown(bp,sizeof(struct socket))) == NULLBUF){
  227.         /* No space, drop whole packet */
  228.         free_p(bp);
  229.         udpInErrors++;
  230.         return;
  231.     }
  232.     fsocket = (struct socket *)packet->data;
  233.     fsocket->address = ip->source;
  234.     fsocket->port = udp.source;
  235.  
  236.     /* Queue it */
  237.     enqueue(&up->rcvq,packet);
  238.     up->rcvcnt++;
  239.     udpInDatagrams++;
  240.     if(up->r_upcall)
  241.         (*up->r_upcall)(iface,up,up->rcvcnt);
  242. }
  243. /* Look up UDP socket, return control block pointer or NULLUDP if nonexistant */
  244. static
  245. struct udp_cb *
  246. lookup_udp(socket)
  247. struct socket *socket;
  248. {
  249.     register struct udp_cb *up;
  250.     
  251.     for(up = Udps[hash_udp(socket)];up != NULLUDP;up = up->next){
  252.         if(socket->port == up->socket.port
  253.          && (socket->address == up->socket.address
  254.              || up->socket.address == INADDR_ANY))
  255.             break;
  256.     }
  257.     return up;
  258. }
  259.  
  260. /* Hash a UDP socket (address and port) structure */
  261. static
  262. int16
  263. hash_udp(socket)
  264. struct socket *socket;
  265. {
  266.     /* Hash depends only on port number, to make addr wildcarding work */
  267.     return (int16)(socket->port % NUDP);
  268. }
  269.